'use strict';

(function(){
    var module = angular.module('AppCheck.Dashboard',[
        "ui.router",
        "restangular",
        "services.utils.RestAngularHandler",
        "services.Utils",
        "directives.TestSummary"
    ]);


    module.controller('dashboardCtrl', [
        "$scope",
        "$http",
        "RestAngularHandlerFactory",
        "Restangular",
        "$state",
        "utils",
        function($scope, $http, restAngularHandlerFactory, restangular, $state, utils) {
            var init = function(){
                // store the flag variable indicating the status of the http call.
                $scope.rest = {};
                $scope.testIds = [];
                $scope.title = $state.current.data.displayName;
                $scope.getTestIds(0, 20);
            };
            $scope.getTestIds = function(start, end){
                $scope.testIds = [];

                var newTestRESTHandler = restAngularHandlerFactory.createAndStart($scope.rest);
                restangular.one("test-plan").one("start", start).one("end", end).get().then(
                    newTestRESTHandler.getSuccessCallback(
                        function (result) {
                            for(var i = 0; i < result.length; i++){
                                $scope.testIds.unshift(result[i]._id);
                            }
                            $scope.testIdsInitialized = true;
                        }
                    ),
                    newTestRESTHandler.getErrorCallback(
                        //TODO: handle the error
                        function (errorResponse) {
                            console.error(errorResponse);
                        }
                    )
                );
            };

            $scope.refreshSelect = function (last) {
                if(last) {
                    $('.selectpicker').selectpicker('refresh');
                    // console.log("x");
                }
            };

            init();
    }]);
})(this);
